<?php

namespace app\common\model;

use think\Cache;
use think\Model;

/**
 * 仓库基础资料录入数据模型
 */
class InvShelvesLatest extends Model
{
     protected $name = 'inv_shelves_latest';
     public static function addData(){
         //更新物料最新货架信息 
         /*进周转区的都不要统计进来*/
         $wms_kc_rows = db('wms_kc')->query('SELECT (select id from fa_inventory inv where inv.number=mx.cInvCode) inv_id,
mx.warehouse_id,mx.shelves_id
FROM fa_kcmx mx
INNER JOIN (
	SELECT
		cInvCode,
		MAX(id) AS max_id
	FROM
		fa_kcmx
	WHERE
		type=1 and shelves_id not in (3,4) 
	GROUP BY
		cInvCode
) AS subquery ON mx.cInvCode = subquery.cInvCode
AND mx.id = subquery.max_id 
ORDER BY mx.id DESC');
         
         // 过滤后的结果数组,不然得在外部查询，在做一次子查询，耗性能
         $wms_kc_rows = array_values(array_filter($wms_kc_rows, function ($item) {
             return $item['inv_id'] !== null;
         }));
         
         db('inv_shelves_latest')->execute('TRUNCATE TABLE fa_inv_shelves_latest');
         db('inv_shelves_latest')->insertAll($wms_kc_rows);
         
         //file_put_contents('./uploads/data/invShelvesLastest.json', json_encode($wms_kc_rows, true));
         return $wms_kc_rows;
     }
     
     public function shelves(){
         return $this->hasOne('\app\admin\model\information\Shelves','id','shelves_id','','LEFT')->setEagerlyType(0);
     }
     
     public function warehouse(){
         return $this->hasOne('\app\admin\model\information\Warehouse','id','warehouse_id','','LEFT')->setEagerlyType(0);
     }
}
